package com.hzb.springcloud.controller;

import com.hzb.springcloud.entities.CommonResult;
import com.hzb.springcloud.entities.Payment;
import com.hzb.springcloud.service.IPaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @version v1.0
 * @ProjectName: cloud2020
 * @ClassName: PaymentController
 * @Description: TODO(一句话描述该类的功能)
 * @Author: huangZB
 * @Date: 2020/3/12 16:35
 */
@RestController
public class PaymentController {


    @Resource
    private IPaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;


    /**
     * @Title:
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @param
     * @return @return
     * @author huangZB
     * @throws
     * @date
     */
    @RequestMapping(value = "/payment/creat")
    public CommonResult<Payment> create( @RequestBody Payment payment){
        int i = paymentService.create(payment);
        if (i >0) {
            return new CommonResult(200, "插入成功", i);
        }else {
            return new CommonResult(500, "插入失败", null);
        }
    }

    /**
     * @Title:
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param @param
     * @return @return
     * @author huangZB
     * @throws
     * @date
     */
    @GetMapping(value = "/payment/getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable("id") String  id){
        Payment payment = paymentService.getPaymentById(id);
        if (payment != null) {
            return new CommonResult(200, "查询成功", payment);
        }else {
            return new CommonResult(500, "查询失败", null);
        }
    }

    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}

        return serverPort;
    }

    @GetMapping(value = "/payment/lb")
    public String lb(){

        return serverPort;
    }


    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        return "hi ,i'am paymentzipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
    }

}
